﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ft_module_parser
{
    class pv_dbs_raw
    {
        public List<pv_raw> pv_raw = new List<pv_raw>();
        public List<pv_raw> mdata_pv_raw = new List<pv_raw>();
        public void load(string filepath)
        {
            using (StreamReader file = new StreamReader(filepath))
            {
                while (file.Peek() >= 0)
                {
                    string line = file.ReadLine();
                    if (line.StartsWith("pv_"))
                    {
                        string[] line2 = line.Split('.');
                        string[] line3 = line2[0].Split('_');

                        int pvid = int.Parse(line3[1]);

                        int lastDiff = 0;

                        var curRawPv = pv_raw.Where(c => c.pvId == pvid).FirstOrDefault();
                        if (curRawPv == null)
                        {
                            pv_raw.Add(new pv_raw(pvid));
                            curRawPv = pv_raw.Where(c => c.pvId == pvid).FirstOrDefault();
                        }
                        curRawPv.rawrows.Add(line);
                    }
                }
            }
        }

        public void loadmdata(string filepath)
        {
            using (StreamReader file = new StreamReader(filepath))
            {
                while (file.Peek() >= 0)
                {
                    string line = file.ReadLine();
                    if (line.StartsWith("pv_"))
                    {
                        string[] line2 = line.Split('.');
                        string[] line3 = line2[0].Split('_');

                        int pvid = int.Parse(line3[1]);

                        int lastDiff = 0;

                        var curRawPv = mdata_pv_raw.Where(c => c.pvId == pvid).FirstOrDefault();
                        if (curRawPv == null)
                        {
                            mdata_pv_raw.Add(new pv_raw(pvid));
                            curRawPv = mdata_pv_raw.Where(c => c.pvId == pvid).FirstOrDefault();
                        }
                        curRawPv.rawrows.Add(line);
                    }
                }
            }
        }

        public int merge(string s, pv_lists_raw pv_list, int force_pvid = -1)
        {
            int id = 1;
            var test = pv_raw.Where(c => c.pvId == id).FirstOrDefault();
            var test2 = mdata_pv_raw.Where(c => c.pvId == id).FirstOrDefault();
            while ((test != null) || (test2 != null))
            {
                id++;
                test = pv_raw.Where(c => c.pvId == id).FirstOrDefault();
                test2 = pv_raw.Where(c => c.pvId == id).FirstOrDefault();
            }

            if (force_pvid != -1)
                id = force_pvid;

            bool easy = false, normal = false, hard = false, extreme = false;

            string date = "";
            string songname = "";

            StreamReader sr = new StreamReader(s);
            while (sr.Peek() > -1)
            {
                string line = sr.ReadLine();
                if ((!line.StartsWith("#")) && (line.Count() != 0))
                {
                    string[] line2 = line.Split('.');
                    string[] line3 = line2[0].Split('_');

                    int pvid = int.Parse(line3[1]);

                    if (line.Contains("song_name="))
                    {
                        string[] line4 = line.Split('=');
                        songname = line4[1];
                    }

                    if (line.Contains("date="))
                    {
                        string[] line4 = line.Split('=');
                        date = line4[1];
                    }

                    var curRawPv = pv_raw.Where(c => c.pvId == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        pv_raw.Add(new pv_raw(id));
                        curRawPv = pv_raw.Where(c => c.pvId == id).FirstOrDefault();
                        
                        if (line.Contains("easy.script"))
                            easy = true;
                        if (line.Contains("normal.script"))
                            normal = true;
                        if (line.Contains("hard.script"))
                            hard = true;
                        if (line.Contains("extreme.script"))
                            extreme = true;

                        if (line.Contains("easy.0.script"))
                            easy = true;
                        if (line.Contains("normal.0.script"))
                            normal = true;
                        if (line.Contains("hard.0.script"))
                            hard = true;
                        if (line.Contains("extreme.0.script"))
                            extreme = true;

                        line = line.Remove(0, 6);
                        line = "pv_" + string.Format("{0:000}", id) + line;
                        curRawPv.rawrows.Add(line);

                    }
                    else
                    {
                        if (line.Contains("easy.script"))
                            easy = true;
                        if (line.Contains("normal.script"))
                            normal = true;
                        if (line.Contains("hard.script"))
                            hard = true;
                        if (line.Contains("extreme.script"))
                            extreme = true;

                        if (line.Contains("easy.0.script"))
                            easy = true;
                        if (line.Contains("normal.0.script"))
                            normal = true;
                        if (line.Contains("hard.0.script"))
                            hard = true;
                        if (line.Contains("extreme.0.script"))
                            extreme = true;

                        line = line.Remove(0, 6);
                        line = "pv_" + string.Format("{0:000}", id) + line;
                        curRawPv.rawrows.Add(line);
                    }
                }
            }
            sr.Close();

            if (pv_list.game_type == 0)
            {
                int count = pv_list.pv_list.Count;
                pv_list.pv_list.Add(new pv_list_raw(pv_list.pv_list.Count));
                var curlist = pv_list.pv_list.Where(c => c.index == count).First();
                //var header = "pv_list." + count + ".";
                var header = "";
                curlist.line.Add(header + "adv_demo_ed_day=1");
                curlist.line.Add(header + "adv_demo_ed_month=1");
                curlist.line.Add(header + "adv_demo_ed_year=2000");
                curlist.line.Add(header + "adv_demo_st_day=1");
                curlist.line.Add(header + "adv_demo_st_month=1");
                curlist.line.Add(header + "adv_demo_st_year=2029");

                if (!easy)
                {
                    curlist.line.Add(header + "easy_ed_day=-1");
                    curlist.line.Add(header + "easy_ed_month=-1");
                    curlist.line.Add(header + "easy_ed_year=-1");
                    curlist.line.Add(header + "easy_st_day=-1");
                    curlist.line.Add(header + "easy_st_month=-1");
                    curlist.line.Add(header + "easy_st_year=-1");
                    curlist.line.Add(header + "easy_ver=-1");
                }
                else
                {
                    curlist.line.Add(header + "easy_ed_day=1");
                    curlist.line.Add(header + "easy_ed_month=1");
                    curlist.line.Add(header + "easy_ed_year=2029");
                    curlist.line.Add(header + "easy_st_day=1");
                    curlist.line.Add(header + "easy_st_month=1");
                    curlist.line.Add(header + "easy_st_year=2000");
                    curlist.line.Add(header + "easy_ver=1");
                }

                curlist.line.Add(header + "encore_ed_day=-1");
                curlist.line.Add(header + "encore_ed_month=-1");
                curlist.line.Add(header + "encore_ed_year=-1");
                curlist.line.Add(header + "encore_st_day=-1");
                curlist.line.Add(header + "encore_st_month=-1");
                curlist.line.Add(header + "encore_st_year=-1");
                curlist.line.Add(header + "encore_ver=-1");

                if (!extreme)
                {
                    curlist.line.Add(header + "extreme_ed_day=-1");
                    curlist.line.Add(header + "extreme_ed_month=-1");
                    curlist.line.Add(header + "extreme_ed_year=-1");
                    curlist.line.Add(header + "extreme_st_day=-1");
                    curlist.line.Add(header + "extreme_st_month=-1");
                    curlist.line.Add(header + "extreme_st_year=-1");
                    curlist.line.Add(header + "extreme_ver=-1");
                }
                else
                {
                    curlist.line.Add(header + "extreme_ed_day=1");
                    curlist.line.Add(header + "extreme_ed_month=1");
                    curlist.line.Add(header + "extreme_ed_year=2029");
                    curlist.line.Add(header + "extreme_st_day=1");
                    curlist.line.Add(header + "extreme_st_month=1");
                    curlist.line.Add(header + "extreme_st_year=2000");
                    curlist.line.Add(header + "extreme_ver=1");
                }

                if (!hard)
                {
                    curlist.line.Add(header + "hard_ed_day=-1");
                    curlist.line.Add(header + "hard_ed_month=-1");
                    curlist.line.Add(header + "hard_ed_year=-1");
                    curlist.line.Add(header + "hard_st_day=-1");
                    curlist.line.Add(header + "hard_st_month=-1");
                    curlist.line.Add(header + "hard_st_year=-1");
                    curlist.line.Add(header + "hard_ver=-1");
                }
                else
                {
                    curlist.line.Add(header + "hard_ed_day=1");
                    curlist.line.Add(header + "hard_ed_month=1");
                    curlist.line.Add(header + "hard_ed_year=2029");
                    curlist.line.Add(header + "hard_st_day=1");
                    curlist.line.Add(header + "hard_st_month=1");
                    curlist.line.Add(header + "hard_st_year=2000");
                    curlist.line.Add(header + "hard_ver=1");
                }
                curlist.line.Add(header + "id=" + id);
                curlist.line.Add(header + "ignore=1");
                curlist.line.Add(header + "name=" + songname);

                if (!normal)
                {
                    curlist.line.Add(header + "normal_ed_day=-1");
                    curlist.line.Add(header + "normal_ed_month=-1");
                    curlist.line.Add(header + "normal_ed_year=-1");
                    curlist.line.Add(header + "normal_st_day=-1");
                    curlist.line.Add(header + "normal_st_month=-1");
                    curlist.line.Add(header + "normal_st_year=-1");
                    curlist.line.Add(header + "normal_ver=-1");
                }
                else
                {
                    curlist.line.Add(header + "normal_ed_day=1");
                    curlist.line.Add(header + "normal_ed_month=1");
                    curlist.line.Add(header + "normal_ed_year=2029");
                    curlist.line.Add(header + "normal_st_day=1");
                    curlist.line.Add(header + "normal_st_month=1");
                    curlist.line.Add(header + "normal_st_year=2000");
                    curlist.line.Add(header + "normal_ver=1");
                }
            }

            if (pv_list.game_type == 1)
            {
                int count = pv_list.pv_list.Count;
                pv_list.pv_list.Add(new pv_list_raw(pv_list.pv_list.Count));
                var curlist = pv_list.pv_list.Where(c => c.index == count).First();
                //var header = "pv_list." + count + ".";
                var header = "";
                curlist.line.Add(header + "adv_demo_ed_day=1");
                curlist.line.Add(header + "adv_demo_ed_month=1");
                curlist.line.Add(header + "adv_demo_ed_year=2000");
                curlist.line.Add(header + "adv_demo_st_day=1");
                curlist.line.Add(header + "adv_demo_st_month=1");
                curlist.line.Add(header + "adv_demo_st_year=2029");

                if (!easy)
                {
                    curlist.line.Add(header + "easy.0.ed_day=-1");
                    curlist.line.Add(header + "easy.0.ed_month=-1");
                    curlist.line.Add(header + "easy.0.ed_year=-1");
                    curlist.line.Add(header + "easy.0.edition=0");
                    curlist.line.Add(header + "easy.0.st_day=-1");
                    curlist.line.Add(header + "easy.0.st_month=-1");
                    curlist.line.Add(header + "easy.0.st_year=-1");
                    curlist.line.Add(header + "easy.0.ver=-1");
                }
                else
                {
                    curlist.line.Add(header + "easy.0.ed_day=1");
                    curlist.line.Add(header + "easy.0.ed_month=1");
                    curlist.line.Add(header + "easy.0.ed_year=2029");
                    curlist.line.Add(header + "easy.0.edition=0");
                    curlist.line.Add(header + "easy.0.st_day=1");
                    curlist.line.Add(header + "easy.0.st_month=1");
                    curlist.line.Add(header + "easy.0.st_year=2000");
                    curlist.line.Add(header + "easy.0.ver=1");
                }
                curlist.line.Add(header + "easy.length=1");

                curlist.line.Add(header + "encore.0.ed_day=-1");
                curlist.line.Add(header + "encore.0.ed_month=-1");
                curlist.line.Add(header + "encore.0.ed_year=-1");
                curlist.line.Add(header + "encore.0.edition=0");
                curlist.line.Add(header + "encore.0.st_day=-1");
                curlist.line.Add(header + "encore.0.st_month=-1");
                curlist.line.Add(header + "encore.0.st_year=-1");
                curlist.line.Add(header + "encore.0.ver=-1");
                curlist.line.Add(header + "encore.length=1");

                if (!extreme)
                {
                    curlist.line.Add(header + "extreme.0.ed_day=-1");
                    curlist.line.Add(header + "extreme.0.ed_month=-1");
                    curlist.line.Add(header + "extreme.0.ed_year=-1");
                    curlist.line.Add(header + "extreme.0.edition=0");
                    curlist.line.Add(header + "extreme.0.st_day=-1");
                    curlist.line.Add(header + "extreme.0.st_month=-1");
                    curlist.line.Add(header + "extreme.0.st_year=-1");
                    curlist.line.Add(header + "extreme.0.ver=-1");
                }
                else
                {
                    curlist.line.Add(header + "extreme.0.ed_day=1");
                    curlist.line.Add(header + "extreme.0.ed_month=1");
                    curlist.line.Add(header + "extreme.0.ed_year=2029");
                    curlist.line.Add(header + "extreme.0.edition=0");
                    curlist.line.Add(header + "extreme.0.st_day=1");
                    curlist.line.Add(header + "extreme.0.st_month=1");
                    curlist.line.Add(header + "extreme.0.st_year=2000");
                    curlist.line.Add(header + "extreme.0.ver=1");
                }
                curlist.line.Add(header + "extreme.length=1");

                if (!hard)
                {
                    curlist.line.Add(header + "hard.0.ed_day=-1");
                    curlist.line.Add(header + "hard.0.ed_month=-1");
                    curlist.line.Add(header + "hard.0.ed_year=-1");
                    curlist.line.Add(header + "hard.0.edition=0");
                    curlist.line.Add(header + "hard.0.st_day=-1");
                    curlist.line.Add(header + "hard.0.st_month=-1");
                    curlist.line.Add(header + "hard.0.st_year=-1");
                    curlist.line.Add(header + "hard.0.ver=-1");
                }
                else
                {
                    curlist.line.Add(header + "hard.0.ed_day=1");
                    curlist.line.Add(header + "hard.0.ed_month=1");
                    curlist.line.Add(header + "hard.0.ed_year=2029");
                    curlist.line.Add(header + "hard.0.edition=0");
                    curlist.line.Add(header + "hard.0.st_day=1");
                    curlist.line.Add(header + "hard.0.st_month=1");
                    curlist.line.Add(header + "hard.0.st_year=2000");
                    curlist.line.Add(header + "hard.0.ver=1");
                }
                curlist.line.Add(header + "hard.length=1");

                curlist.line.Add(header + "id=" + id);
                curlist.line.Add(header + "ignore=1");
                curlist.line.Add(header + "name=" + songname);

                if (!normal)
                {
                    curlist.line.Add(header + "normal.0.ed_day=-1");
                    curlist.line.Add(header + "normal.0.ed_month=-1");
                    curlist.line.Add(header + "normal.0.ed_year=-1");
                    curlist.line.Add(header + "normal.0.edition=0");
                    curlist.line.Add(header + "normal.0.st_day=-1");
                    curlist.line.Add(header + "normal.0.st_month=-1");
                    curlist.line.Add(header + "normal.0.st_year=-1");
                    curlist.line.Add(header + "normal.0.ver=-1");
                }
                else
                {
                    curlist.line.Add(header + "normal.0.ed_day=1");
                    curlist.line.Add(header + "normal.0.ed_month=1");
                    curlist.line.Add(header + "normal.0.ed_year=2029");
                    curlist.line.Add(header + "normal.0.edition=0");
                    curlist.line.Add(header + "normal.0.st_day=1");
                    curlist.line.Add(header + "normal.0.st_month=1");
                    curlist.line.Add(header + "normal.0.st_year=2000");
                    curlist.line.Add(header + "normal.0.ver=1");
                }
                curlist.line.Add(header + "normal.length=1");
            }
            
            return id;
        }

        public void SaveTo(string file)
        {
            StreamWriter outp = new StreamWriter(file);

            var ordered = pv_raw.OrderBy(c => c.pvId);

            foreach (var i in ordered)
            {
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(u);
                }
            }

            outp.Close();
        }
    }
}
